package com.systemic.service.impl;

import com.systemic.mapper.*;
import com.systemic.pojo.*;
import com.systemic.service.SubjectService;
import com.systemic.vo.PageVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: SubjectServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-18 09:48
 * @Version:
 **/
@Service
public class SubjectServiceImpl implements SubjectService {

    @Resource
    private DynamicMapper dynamicMapper;
    @Resource
    private DynamicImageMapper dynamicImageMapper;
    @Resource
    private SubjectMapper subjectMapper;
    @Resource
    private SubjectDynamicMapper subjectDynamicMapper;
    @Resource
    private SubjectFollowerMapper subjectFollowerMapper;


    private final Integer status = 1;  // 状态值1 表示动态，用来执行查询


    /**
     * 创建话题
     * @param subject
     * @return
     */
    @Override
    @Transactional
    public int create(Subject subject) {
        int row = 0;
        if (subject.getSname()!=null){
            Date date = new Date();
            subject.setGmtCreate(date);
            subject.setGmtModified(date);
            row = subjectMapper.insert(subject);
            if (row==0) {
                throw new RuntimeException("新增失败");
            }
        }
        return row;
    }

    /**
     * 删除话题
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public int deleteList(Integer[] ids) {
        int row = 0;
        // 删除话题的动态关联、关注用户
        if (ids.length!=0){
            for (Integer id:ids) {
                row = subjectFollowerMapper.deleteById(id) + row;  //删除话题的关注用户
                row = subjectDynamicMapper.deleteById(id) + row;  // 删除话题的动态关联
                row = subjectMapper.deleteById(id) + row;  // 删除话题
            }
        }
        return row;
    }

    /**
     * 查询话题
     * @param sname
     * @return
     */
    @Override
    public List<Subject> selectByName(String sname) {
        List<Subject> subjectList = new ArrayList<>();
        if (sname!=null){
            Subject subject = new Subject();
            subject.setSname(sname);
            subjectList = subjectMapper.likeSelect(subject);
        }
        return subjectList;
    }

    /**
     * 关注话题
     * @param subjectFollower
     * @return
     */
    @Override
    @Transactional
    public int follow(SubjectFollower subjectFollower) {
        int row = 0;
        int fans = 0;
        Subject sub = new Subject();
        if (subjectFollower!=null){
            row = subjectFollowerMapper.insert(subjectFollower) + row;
            if (row!=0){
                Subject subject = subjectMapper.selectById(subjectFollower.getSid());  // 根据话题id查询话题
                if (subject!=null){
                    if (subject.getFollowerSum()!=null){
                        fans = subject.getFollowerSum() + 1;   // 拿到关注人数,递增
                        sub.setFollowerSum(fans);   // 写入递增的关注人数
                    }else{
                        sub.setFollowerSum(1);
                    }
                    sub.setId(subject.getId());
                    row = subjectMapper.update(sub)+ row;
                }
            }
        }
        if (row==0){
            throw new RuntimeException("关注失败");
        }
        return row;
    }

    /**
     * 取消关注话题
     * @param subjectFollower
     * @return
     */
    @Override
    @Transactional
    public int unfollow(SubjectFollower subjectFollower) {
        int row = 0;
        int fans = 0;
        Subject sub = new Subject();
        if (subjectFollower!=null){
            row = subjectFollowerMapper.delete(subjectFollower) + row;
            if (row!=0){
                Subject subject = subjectMapper.selectById(subjectFollower.getSid());  // 根据话题id查询话题
                if (subject!=null){
                    if (subject.getFollowerSum()!=null){
                        fans = subject.getFollowerSum() - 1;   // 拿到关注人数,递减
                        sub.setFollowerSum(fans);   // 写入递减的关注人数
                    }else{
                        sub.setFollowerSum(0);
                    }
                    sub.setId(subject.getId());
                    row = subjectMapper.update(sub)+ row;
                }
            }
        }
        if (row==0){
            throw new RuntimeException("取消关注失败");
        }
        return row;
    }

    /**
     * 话题显示动态
     * @param pageVo
     * @return
     */
    @Override
    public List<Dynamic> dynamic(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        // 先拿到用户id
        Integer uid = pageVo.getUid();

        // 根据用户id查询用户关注了哪些话题，拿到话题id
        List<SubjectFollower> subjectFollowerList = subjectFollowerMapper.selectByUid(uid);

        List<SubjectDynamic> subjectDynamics = new ArrayList<>();
        // 根据话题id查询关联的动态 分页查询
        for (SubjectFollower subjectFollower:subjectFollowerList) {
            subjectDynamics = subjectDynamicMapper.selectListPageById(subjectFollower.getSid(), status, offset, pageSize);
        }

        List<Dynamic> dynamicList = new ArrayList<>();
        // 拿到动态的id查询动态
        for (SubjectDynamic sub:subjectDynamics) {
            Dynamic dynamic = dynamicMapper.selectById(sub.getDid());
            DynamicImage image = dynamicImageMapper.selectByDid(sub.getDid(), status);
            if (image != null){
                dynamic.setDynamicImage(image);
                dynamicList.add(dynamic);
            }else{
                dynamicList.add(dynamic);
            }
        }
        return dynamicList;
    }
}
